package com.spring.cloud.study.utils.security;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/3/5
 * Time: 14:54
 * To change this template use File | Settings | File Templates.
 * Description:
 */
public class PrincipalUtils {


    public static String getUserName() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication.getName();
    }

    public static Set getUserOptionPermission() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) authentication.getAuthorities();

        Set<String> collect = new HashSet<>(
                Arrays.asList(
                        authorities.parallelStream()
                                .map(SimpleGrantedAuthority::getAuthority)
                                .collect(Collectors.joining(";"))
                                .split(";")));
        return collect;
    }


}
